<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li v-for="(subject,index) in subjects" :key="index" :class="{active:oneIndex==index}">
                  <a :title="subject.title" href="javascript: void(0);" @click="selectedSubjct(subject.id,index)">{{subject.title}}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"></span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix" style="height: 39px;">
                <li v-for="(sonSubject,index) in sonSubjects" :key="index" :class="{active:twoIndex==index}">
                  <a :title="sonSubject.title" href="javascript: void(0);" @click="selectedSonSubject(sonSubject.id,index)">{{sonSubject.title}}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">3</i>
            </span>
          </section>
          <section class="fl">
            <ol class="js-tap clearfix">
              <li :class="{active:courseQuery.buyCountSort=='' && courseQuery.gmtCreateSort=='' && courseQuery.priceSort==''}">
                <a title="默认排序" href="javascript: void(0);" @click="defaultSort">默认排序
                </a>
              </li>
              <li :class="{active:courseQuery.buyCountSort!=''}" style="width: 118px;">
                <a title="关注度" href="javascript: void(0);" @click="countSort">关注度
                  <span v-if="courseQuery.buyCountSort===1">&nbsp;↓</span>
                  <span v-if="courseQuery.buyCountSort===2">&nbsp;↑</span>
                </a>
              </li>
              <li :class="{active:courseQuery.gmtCreateSort!=''}" style="width: 118px;">
                <a title="时间" href="javascript: void(0);" @click="timeSort">时间
                  <span v-if="courseQuery.gmtCreateSort===1">&nbsp;↓</span>
                  <span v-if="courseQuery.gmtCreateSort===2">&nbsp;↑</span>
                </a>
              </li>
              <li :class="{active:courseQuery.priceSort!=''}" style="width: 118px;">
                <a title="价格" href="javascript: void(0);" @click="priceSort">价格
                  <span v-if="courseQuery.priceSort===1">&nbsp;↓</span>
                  <span v-if="courseQuery.priceSort===2">&nbsp;↑</span>
                </a>
              </li>
            </ol>
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-if="courses.items==null || courses.items.length==0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam">没有相关数据，正在努力整理中...</span>
          </section>
          <!-- /无数据提示 结束-->
          <article class="comm-course-list">
            <ul class="of" id="bna">
              <li v-for="course in courses.items" :key="course.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img :src="course.cover" class="img-responsive" :alt="course.title">
                    <div class="cc-mask">
                      <a :href="'/course/'+course.id" :title="course.title" class="comm-btn c-btn-1">开始学习</a>
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a :href="'/course/'+course.id" :title="course.title" class="course-title fsize18 c-333">{{course.title}}</a>
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span class="fr jgTag bg-green">
                      <i class="c-fff fsize12 f-fA">{{course.price==0?'免费':'收费'}}</i>
                    </span>
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">9634人学习</i>
                      |
                      <i class="c-999 f-fA">9634评论</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"></div>
          </article>
        </div>
        <!-- 公共分页 开始 -->
        <div>
          <div class="paging">
            <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
            <a
              :class="{undisable: !courses.hasPrevious}"
              href="#"
              title="首页"
              @click.prevent="gotoPage(1)">首</a>
            <a
              :class="{undisable: !courses.hasPrevious}"
              href="#"
              title="前一页"
              @click.prevent="gotoPage(courses.current-1)">&lt;</a>
            <a
              v-for="page in courses.pages"
              :key="page"
              :class="{current: courses.current == page, undisable: courses.current == page}"
              :title="'第'+page+'页'"
              href="#"
              @click.prevent="gotoPage(page)">{{ page }}</a>
            <a
              :class="{undisable: !courses.hasNext}"
              href="#"
              title="后一页"
              @click.prevent="gotoPage(courses.current+1)">&gt;</a>
            <a
              :class="{undisable: !courses.hasNext}"
              href="#"
              title="末页"
              @click.prevent="gotoPage(courses.pages)">末</a>
            <div class="clear"/>
          </div>
        </div>
        <!-- 公共分页 结束 -->
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>

  import courseApi from '@/api/course'

  export default {

    data() {
      return {
        // 查询对象
        "courseQuery": {
          "teacherId": "",
          "subjectParentId": "",
          "subjectId": "",
          "buyCountSort": "",
          "gmtCreateSort": "",
          "priceSort": "",
          "current": 1,
          "limit": 8
        },
        // 课程实体对象
        "courses": "",
        // 课程分类
        "subjects": "",
        // 二级分类
        "sonSubjects": "",
        "oneIndex": -1,
        "twoIndex": -1
      };
    },
    created() {
      this.current=1;
      // 查询课程信息
      this.selectCourse();
      // 查询课程分类信息
      this.selectSubject();

      // 初始化参数
      // this.courseQuery.subjectParentId="";
      // this.courseQuery.subjectId="";
      // this.courseQuery.buyCountSort="";
      // this.courseQuery.gmtCreateSort="";
      // this.courseQuery.priceSort="";
      // this.courseQuery.current=1;
      // this.courseQuery.limit=8;
      
    },
    methods: {
      selectCourse() {
        courseApi.selectCoursesByCourseVo(this.courseQuery)
          .then(res => {
            if(res.data.code==200) {
              this.courses=res.data.data;
            }else {
              this.$message.error(res.data.message);
            }
          });
      },
      // 跳转页动作
      gotoPage(page) {
        // 当page<=0或page>=pages时，提示
        if(page<=0) {
          this.$message.error("当前为第 1 页");
          return ;
        }else if(page>this.courses.pages) {
          this.$message.error("当前为最后 1 页");
          return ;
        }
        this.courseQuery.current=page;
        this.selectCourse();
      },
      // 获取课程一级和二级分类
      selectSubject() {
        courseApi.selectSubject()
          .then(res => {
            if(res.data.code==200) {
              this.subjects=res.data.data.subject;
            }else {
              this.$message.error(res.data.message);
            }
          });
      },
      // 根据一级课程选中情况显示二级分类
      selectedSubjct(subjectId,index) {
        if(this.subjects.length<=index || index<0) {
          this.$message.error("一级课程分类索引异常");
          return ;
        }
        // 若当前选中于点击选中相同
        if(this.oneIndex==index) {
          this.oneIndex=-1;
          // 初始化参数
          this.courseQuery.subjectParentId="";
          this.sonSubjects="";
        }else {
          // 初始化参数
          this.courseQuery.subjectParentId=subjectId;
          // 选中情况
          this.oneIndex=index;
          this.sonSubjects=this.subjects[index].children;
        }
        // 初始化参数
        this.courseQuery.subjectId="";
        this.courseQuery.buyCountSort="";
        this.courseQuery.gmtCreateSort="";
        this.courseQuery.priceSort="";
        this.courseQuery.current=1;
        this.courseQuery.limit=8;
        // 选中情况
        this.twoIndex=-1;

        // 执行查询操作
        this.selectCourse();
      },
      // 选中二级分类
      selectedSonSubject(sonSubjectId,index) {
        if(this.sonSubjects.length<=index || index<0) {
          this.$message.error("二级课程分类索引异常");
          return ;
        }
        // 若当前选中于点击选中相同
        if(this.twoIndex==index) {
          this.twoIndex=-1;
          // 初始化参数
          this.courseQuery.subjectId="";
        }else {
          // 初始化参数
          this.courseQuery.subjectId=sonSubjectId;
          // 选中情况
          this.twoIndex=index;
        }
        this.courseQuery.buyCountSort="";
        this.courseQuery.gmtCreateSort="";
        this.courseQuery.priceSort="";
        this.courseQuery.current=1;
        this.courseQuery.limit=8;
        // 执行查询操作
        this.selectCourse();
      },
      // 清除一级以及二级课程分类的选中情况
      clearSubject() {
        this.courseQuery.subjectParentId="";
        this.courseQuery.subjectId="";
        // 将二级分类置为空
        // 初始化参数
        this.courseQuery.subjectParentId="";
        this.courseQuery.subjectId="";
        this.courseQuery.buyCountSort="";
        this.courseQuery.gmtCreateSort="";
        this.courseQuery.priceSort="";
        this.courseQuery.current=1;
        this.courseQuery.limit=8;
        // 执行查询操作
        this.selectCourse();
      },
      // 默认排序
      defaultSort() {
        this.courseQuery.buyCountSort="";
        this.courseQuery.gmtCreateSort="";
        this.courseQuery.priceSort="";
        this.courseQuery.current=1;
        this.courseQuery.limit=8;
        // 执行查询操作
        this.selectCourse();
      },
      // 关注度排序
      countSort() {
        // 关注度排序为空 => 关注度排序为降序
        if(!this.courseQuery.buyCountSort) {
          this.courseQuery.buyCountSort=1;
        } else if(this.courseQuery.buyCountSort==1) {
          // 关注度排序为降序 => 关注度排序为升序
          this.courseQuery.buyCountSort=2;
        } else {
          // 关注度排序为升序 => 关注度排序为空
          this.courseQuery.buyCountSort="";
        }
        this.courseQuery.current=1;
        this.courseQuery.limit=8;
        // 执行查询操作
        this.selectCourse();
      },
      // 时间排序
      timeSort() {
        // 时间度排序为空 => 时间度排序为降序
        if(!this.courseQuery.gmtCreateSort) {
          this.courseQuery.gmtCreateSort=1;
        } else if(this.courseQuery.gmtCreateSort==1) {
          // 时间度排序为降序 => 时间度排序为升序
          this.courseQuery.gmtCreateSort=2;
        } else {
          // 时间度排序为升序 => 时间度排序为空
          this.courseQuery.gmtCreateSort="";
        }
        this.courseQuery.current=1;
        this.courseQuery.limit=8;
        // 执行查询操作
        this.selectCourse();
      },
      // 价格排序
      priceSort() {
        // 价格度排序为空 => 价格度排序为降序
        if(!this.courseQuery.priceSort) {
          this.courseQuery.priceSort=1;
        } else if(this.courseQuery.priceSort==1) {
          // 价格度排序为降序 => 价格度排序为升序
          this.courseQuery.priceSort=2;
        } else {
          // 价格度排序为升序 => 价格度排序为空
          this.courseQuery.priceSort="";
        }
        this.courseQuery.current=1;
        this.courseQuery.limit=8;
        // 执行查询操作
        this.selectCourse();
      }
    }

  }

</script>

<style scoped>
  .active {
    background: orange;
  }
  .hide {
    display: none;
  }
  .show {
    display: block;
  }
</style>
